function refresh(){
    $("#dataTable").bootstrapTable("removeAll");
    $("#dataTable").bootstrapTable('refresh');
}
//搜索以前将页码换成第一页
function searchBeforeInit(){
    var options=$("#dataTable").bootstrapTable("getOptions");
    options.pageNumber=1;
    $("#dataTable").bootstrapTable('refreshOptions',options);
}
function initSearchEvent(){
	
    $("#serachBtn").click(function(){
    	searchBeforeInit();
        refresh();
    });
    $('#serachBtn').bind('keypress',function(e){
       

        if(e.keyCode == 13){
        	searchBeforeInit();
            refresh();
        }
    });
    $('#samplingCode').bind('keypress',function(e){
        
        if(e.keyCode == 13){
        	searchBeforeInit();
            refresh();
        }
    });
    $('#samplingName').bind('keypress',function(e){
       
        if(e.keyCode == 13){
        	searchBeforeInit();
            refresh();
        }
    });
	
}
function editData(rowId){
    layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '420px'],
        content: 'sampling_update.html?rowId='+rowId,
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret= openWin.yesOperator();
            if(ret){
                layer.close(index);
                refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}
function deleteData(){
	layer.confirm('确定要删除这条数据吗？',
            {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
                var rows= $("#dataTable").bootstrapTable('getSelections');
                var rowIds = [];
                for (var i = 0; i < rows.length; i++) {
                    rowIds.push(rows[i].id);
                }
                $$.ajax({
                    url: "/rest/IotSamplingBusiness/remove",
                    data: {
                        id : rowIds.join(',')
                    },
                    success: function() {
                        refresh();
                    }
                });
                layer.close(index);
            });
}
function addData(){
	layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '420px'],
        content: 'sampling_add.html',
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret= openWin.yesOperator();
            if(ret){
               layer.close(index);
               refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}
$(function () {
    //$('#deviceCode').focus();
	
	initSearchEvent();
    //重置表单
    $("#searchReset").on("click",function(){
        //$("#searchForm")[0].reset();
        $("#searchForm input[type='text']").val("");
    });
    //渲染表格
    var columns = deviceList.columnsInit();
    var table = deviceList.gridInit(columns);

    $("#del-btn").click(function(){
        var len = $("#dataTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据',  {time:1000});
        } else {
        	deleteData();
            
        }

    });
    initSearchEvent();
    //新建弹框
    $("#add-btn").on('click',function () {
    	addData();
    });
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var deviceList = {
        columnsInit:function(){
            return  [
            	{
                    field: 'status',
                    title: '序号',
                    formatter: function (value, row, index) {
                            return     index+1 ;
                    }
                },
            	{field:"samplingCode",title:"采样编码" },  
                {field:"samplingName",title:"采样名称" },
                {field:"samplingCycle",title:"采样周期" },
                {field:"rowSort",title:"排序号" },
                {field:"remarks",title:"备注" },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if(value == '1'){
                            return [
                                '<span class="sys-type-bg pro-status-green">启用</span>'
                            ];
                        }
                        return [
                            '<span class="sys-type-bg pro-status-red">停用</span>'
                        ];
                    }
                },
            {
                field: 'optionDel',
                checkbox: true
            },{
                field: 'option',
                title: '操作',
                width: 80,
                align: 'center',
                formatter: function (value, row, index) {
                    /*//up菜单向上展示、 down向下展示
                    var pageSize = deviceList.pageSize;
                    var cssStyle = 'bottom';
                    if (index > pageSize / 3) {
                        cssStyle = 'top';
                    }*/
                    return [
                        '<li><a href="javascript:void(0)" onclick="editData(\''+row.id+'\');" title="编辑" class="opt-icon"><i class="glyphicon glyphicon-pencil"></i></a></li>'
                    ].join('');
                }

            }]
        },
         gridInit:function(columns){

            return $("#dataTable").bootstrapTable({
                method:"post",
                contentType:"application/x-www-form-urlencoded",
                url:$$.restServerUrl+'/bootstrap/basegrid/IotSamplingBusiness/list',
                columns: columns,
                undefinedText:'-',
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                clickToSelect: true,
                singleSelect:false,
                sidePagination: "server",
                queryParamsType : "page",
                sortSide:'client',
                sortable:true,
                pageNumber: 1,
                pageSize:10,
                idField:'id',
                silentSort: true,
                pageList: [
                	10, 20, 50, 100
                ],
                onClickRow:function(row, $element,field){
                     $("#dataTable").bootstrapTable("uncheckAll");
                },
                queryParams: function queryParams(params) {   //设置查询参数

                var args={};
                args.c_samplingCode_7=$("#samplingCode").val();
                args.c_samplingName_7=$("#samplingName").val();
                args.page=params.pageNumber-1; //    params.pageNumber
                args.pageSize=params.pageSize;     //params.pageSize
                args.isPage=1;
                //args.order=orderStr;
                var paramStr=JSON.stringify(args);

                //alert(params.pageNumber+":"+params.pageSize);
                //alert(orderStr);
                var param = {
                    params:paramStr
                };
                return param;
            },
                onPostBody:function () {
                    $(".opt-dropdown").each(function(index,data){
                        var _this = this;
                        $(this).hover(function(){
                            $(_this).find(".opt-dropdown-menu").show();
                        },function(){
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                }
            })

        },
        dataInit:function(){
            return  []
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = deviceList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return deviceList;});
    } else {
        !('plugin' in _global) && (_global.deviceList = deviceList);
    }
}());
